<?php
declare (strict_types=1);

namespace app\scan_bag\controller;

use app\admin\controller\iot\ScanBagStrategy;
use app\admin\model\base\User;
use app\admin\model\iot\Device;
use app\admin\model\iot\ScanBagDay;
use app\admin\model\iot\ScanBagWay;
use app\admin\model\iot\ScanBagStrategy as ScanBagStrategyModel;
use app\BaseController;
use app\scan_bag\service\ScanBagV1Service;
use app\scan_bag\service\ScanBagV2Service;
use app\scan_bag\service\V1_Way_1_0Service;
use think\facade\Cookie;
use think\facade\Log;
use think\facade\View;
use think\response\Json;
use wechat\WsOfficialAccount;

/**
 * 扫码取袋首页
 */
class TakeBag extends BaseController
{

    private string $CookieKey = 'cookie_takebag_way';
    private string $CookieStrategyKey = 'cookie_strategy_user';

    /**
     * 取袋扫聚合码开始
     * @return string
     */
    public function index(): string
    {
        date_default_timezone_set("Asia/Shanghai");
        die;
        //接收参数
        $domain = $this->request->domain();
        $mac_id = $this->request->param('mac_id/s', '');
        $mac_arr = [6610244,6610237,6610235, 6610233, 6610211];
        if(in_array($mac_id,$mac_arr)){
            $mac_id = "";
        }
        //停掉部分设备扫码功能（环保袋）
        $mac_arr = [81925, 81926, 81927, 81928, 81929, 81930, 81924, 81923, 81901, 81900, 81899, 81898, 81897, 81896, 81895, 81894, 81893, 81892, 81891, 81890, 81889, 81888, 81887, 81883, 81882, 81872, 81867, 81866, 81861, 81860, 81858, 81857, 81850, 81847, 81845, 81844, 81843, 81842, 81841, 81840, 81839, 81838, 81837, 81836, 81835, 81834, 81833, 81832, 81831, 81830, 81829, 81828, 81827, 81826, 81825, 81824, 81823, 81822, 81821, 81820, 81819, 81818, 81817, 80085, 80209, 80237, 80664, 80873, 81064, 81374, 82235, 80184, 80820, 81063, 81665, 80165, 80313, 81317, 81120, 80214, 81635, 81671, 82234, 82258, 80059, 80063, 80106, 80301, 80785, 81002, 81444, 81463, 80821, 81148, 81593, 81664, 82244, 80038, 80767, 80791, 81482, 82182, 82243, 82248, 80070, 80072, 80662, 80781, 80802, 81086, 81142, 81347, 81399, 81597, 82201, 82266, 80159, 80737, 82199, 82247, 82267, 82195, 80951, 81085, 81366, 82204, 82285, 80270, 82179, 82180, 82257, 82268, 82269, 80058, 80954, 80176, 82217, 81634, 81639, 80110, 80179, 80223, 82228, 82246, 82259, 80071, 80077, 80156, 80356, 80623, 80628, 81019, 81342, 82181, 80743, 81020, 81345, 82287, 80153, 80040, 80158, 80617, 80624, 80760, 80162, 80182, 80379, 80629, 81052, 81180, 81643, 82205, 82270, 81115, 80799, 80226, 81211, 82242, 82254, 82261, 82264, 80238, 81595, 81549, 80950, 81603, 80155, 80161, 80242, 81601, 81632, 82219, 80108, 80227, 81039, 82260, 80148, 80157, 80381, 80668, 81041, 81594, 80076, 80181, 80394, 81103, 82262, 80075, 80136, 81125, 80618, 80357, 81333, 82206, 81131, 82245, 81066, 81116, 82203, 81205, 82198, 82289, 81069, 81373, 81062, 81661, 81663, 80189, 80100, 80138, 80955, 80235, 80112, 80232, 80102, 80180, 80659, 82200, 82265, 80578, 82218, 80061, 80222, 80359, 81072, 82253, 80300, 81042, 82216, 82278, 81073, 81443, 81092, 81110, 81445, 81114, 80107, 80537, 82207, 80067, 80079, 80205, 81068, 81548, 80431, 80536, 80081, 80173, 80265, 80630, 81489, 81602, 82237, 80327, 80069, 80177, 80782, 80218, 81075, 81076, 81162, 81604, 81271, 80105, 80118, 80140, 81658, 80137, 80154, 81059, 81071, 80314, 81067, 80113, 80170, 81027, 81029, 81633, 80062, 81024, 80754, 81060, 81653, 80078, 81668, 80191, 80264, 81367, 80066, 81038, 80195, 80201, 81028, 80124, 81083, 82263, 80141, 80793, 80152, 82286, 82233, 80080, 81386, 81493, 80281, 80331, 82238, 82174, 82173, 82172, 82141, 82140, 81644, 81627, 81418, 81417, 81325, 81280, 81277, 81262, 81088, 81081, 81065, 80806, 80801, 80790, 80765, 80757, 80703, 80690, 80667, 80627, 80620, 80549, 80547, 80539, 80535, 80534, 80382, 80380, 80358, 80350, 80349, 80310, 80309, 80231, 80221, 80210, 80206, 80196, 80193, 80169, 80160, 80147, 80145, 80143, 80133, 80109, 80101, 80099, 80074, 80047, 80358, 80352, 80393, 82226, 82225, 82224, 82215, 82214, 82213, 82192, 82191, 82189, 82188, 82187, 82186, 82184, 82183, 82170, 82169, 81931, 81932, 81933, 81934, 81935, 81922, 81921, 81919, 81918, 81886, 81884, 81881, 81879, 81875, 81874, 81714, 81913, 81914, 81916, 81912, 81915, 81911, 81910, 81902, 81909, 81908, 81878, 82303, 82301, 82300, 82299, 82298, 82289, 82287, 82286, 82285, 82284, 82283, 82282, 82281, 82280, 82278, 82277, 82276, 82275, 82270, 82269, 82268, 82267, 82266, 82265, 82264, 82263, 82262, 82261, 82260, 82259, 82258, 82257, 82256, 82255, 82254, 82253, 82248, 82247, 82246, 82245, 82244, 82243, 82242, 82241, 82240, 82239, 82237, 82236, 82235, 82234, 82233, 82232, 82231, 82230, 82229, 82228, 82226, 82219, 82218, 82217, 82216, 82211, 82210, 82209, 82208, 82207, 82206, 82205, 82204, 82203, 82202, 82201, 82200, 82199, 82198, 82197, 82196, 82195, 82194, 82193, 82185, 82182, 82181, 82180, 82179, 82178, 82177, 82176, 82175, 82171, 82169, 82159, 82158, 82157, 82154, 82153, 82151, 82150, 82141, 82140, 82139, 82127, 82125, 82124, 82122, 82121, 82120, 82119, 82118, 82117, 82116, 82115, 81814, 81809, 81808, 81807, 81806, 81805, 81804, 81803, 81802, 81801, 81800, 81799, 81798, 81797, 81796, 81795, 81792, 81791, 81790, 81789, 81788, 81787, 81786, 81785, 81779, 81778, 81777, 81776, 81771, 81770, 81769, 81768, 81767, 81766, 81765, 81764, 81763, 81762, 81755, 81754, 81753, 81752, 81751, 81750, 81749, 81747, 81746, 81743, 81740, 81739, 81737, 81735, 81734, 81733, 81732, 81731, 81730, 81729, 81728, 81727, 81726, 81725, 81724, 81723, 81722, 81720, 81719, 81717, 81716, 81715, 81714, 81713, 81712, 81711, 81710, 81709, 81708, 81707, 81705, 81702, 81701, 81700, 81699, 81690, 81689, 81688, 81687, 81686, 81677, 81676, 81675, 81674, 81673, 81672, 81671, 81670, 81669, 81668, 81667, 81663, 81660, 81658, 81657, 81655, 81653, 81652, 81650, 81649, 81647, 81646, 81644, 81643, 81642, 81641, 81639, 81638, 81637, 81636, 81635, 81634, 81633, 81631, 81629, 81628, 81626, 81624, 81620, 81619, 81615, 81614, 81613, 81610, 81607, 81606, 81605, 81604, 81603, 81602, 81601, 81600, 81599, 81598, 81597, 81596, 81594, 81593, 81592, 81591, 81590, 81589, 81587, 81586, 81585, 81584, 81583, 81582, 81581, 81579, 81578, 81577, 81576, 81575, 81574, 81573, 81570, 81569, 81568, 81567, 81564, 81563, 81559, 81558, 81557, 81555, 81554, 81553, 81552, 81551, 81549, 81548, 81547, 81546, 81545, 81544, 81543, 81542, 81541, 81540, 81539, 81538, 81536, 81535, 81534, 81532, 81531, 81530, 81526, 81525, 81524, 81523, 81522, 81521, 81520, 81519, 81518, 81517, 81516, 81515, 81514, 81513, 81512, 81511, 81509, 81507, 81506, 81505, 81503, 81501, 81500, 81497, 81495, 81494, 81493, 81492, 81491, 81489, 81488, 81487, 81486, 81485, 81483, 81482, 81481, 81479, 81478, 81477, 81476, 81475, 81474, 81473, 81472, 81470, 81469, 81468, 81467, 81466, 81465, 81464, 81463, 81462, 81461, 81459, 81457, 81456, 81455, 81454, 81453, 81452, 81451, 81450, 81449, 81448, 81447, 81446, 81445, 81444, 81443, 81442, 81441, 81440, 81439, 81438, 81437, 81436, 81435, 81433, 81432, 81431, 81429, 81428, 81427, 81426, 81425, 81424, 81423, 81422, 81421, 81420, 81419, 81418, 81417, 81416, 81415, 81414, 81413, 81412, 81411, 81407, 81405, 81404, 81403, 81402, 81401, 81400, 81399, 81398, 81397, 81396, 81395, 81394, 81393, 81392, 81391, 81389, 81388, 81387, 81386, 81384, 81383, 81382, 81380, 81379, 81377, 81376, 81374, 81373, 81369, 81367, 81366, 81363, 81357, 81347, 81345, 81342, 81340, 81339, 81337, 81333, 81325, 81322, 81321, 81319, 81318, 81317, 81315, 81313, 81312, 81311, 81308, 81299, 81297, 81294, 81285, 81284, 81283, 81282, 81281, 81279, 81274, 81272, 81271, 81265, 81263, 81262, 81261, 81260, 81248, 81232, 81222, 81221, 81220, 81219, 81216, 81211, 81206, 81205, 81202, 81201, 81198, 81197, 81196, 81194, 81193, 81192, 81191, 81189, 81188, 81185, 81184, 81183, 81180, 81179, 81178, 81177, 81176, 81173, 81172, 81170, 81169, 81168, 81167, 81165, 81162, 81161, 81160, 81159, 81158, 81157, 81156, 81155, 81151, 81150, 81149, 81148, 81147, 81145, 81143, 81142, 81141, 81140, 81139, 81138, 81136, 81135, 81134, 81131, 81126, 81125, 81123, 81120, 81116, 81115, 81114, 81110, 81103, 81098, 81096, 81094, 81093, 81092, 81090, 81089, 81087, 81086, 81085, 81084, 81083, 81082, 81076, 81075, 81074, 81073, 81072, 81071, 81070, 81069, 81068, 81067, 81066, 81065, 81064, 81063, 81062, 81061, 81060, 81059, 81056, 81055, 81053, 81052, 81051, 81049, 81048, 81045, 81043, 81041, 81040, 81039, 81038, 81036, 81034, 81031, 81030, 81029, 81028, 81027, 81026, 81024, 81020, 81019, 81018, 81014, 81012, 81011, 81010, 81002, 81001, 80983, 80979, 80977, 80975, 80972, 80971, 80967, 80965, 80964, 80963, 80962, 80960, 80955, 80954, 80953, 80952, 80951, 80950, 80949, 80948, 80947, 80946, 80942, 80940, 80938, 80937, 80935, 80934, 80933, 80932, 80931, 80930, 80929, 80928, 80927, 80926, 80925, 80924, 80923, 80922, 80921, 80920, 80919, 80918, 80917, 80916, 80915, 80914, 80913, 80912, 80911, 80910, 80909, 80908, 80907, 80906, 80905, 80904, 80903, 80900, 80899, 80898, 80897, 80896, 80895, 80894, 80893, 80891, 80889, 80888, 80887, 80886, 80885, 80884, 80883, 80880, 80876, 80874, 80856, 80837, 80836, 80832, 80826, 80825, 80824, 80822, 80821, 80820, 80819, 80817, 80806, 80804, 80803, 80802, 80799, 80798, 80793, 80784, 80783, 80782, 80781, 80780, 80777, 80776, 80775, 80771, 80770, 80769, 80768, 80763, 80760, 80758, 80756, 80755, 80752, 80750, 80748, 80747, 80745, 80744, 80743, 80742, 80740, 80737, 80735, 80734, 80733, 80732, 80731, 80730, 80729, 80728, 80727, 80726, 80725, 80724, 80723, 80722, 80721, 80720, 80719, 80713, 80712, 80709, 80708, 80704, 80703, 80701, 80700, 80699, 80697, 80695, 80693, 80690, 80673, 80672, 80671, 80670, 80669, 80667, 80663, 80662, 80661, 80660, 80659, 80658, 80655, 80653, 80652, 80650, 80648, 80647, 80646, 80644, 80643, 80641, 80640, 80639, 80638, 80637, 80636, 80635, 80634, 80632, 80631, 80630, 80629, 80628, 80627, 80626, 80625, 80623, 80622, 80621, 80619, 80618, 80617, 80615, 80614, 80613, 80608, 80607, 80606, 80605, 80604, 80602, 80601, 80600, 80599, 80598, 80597, 80596, 80595, 80591, 80586, 80584, 80583, 80579, 80578, 80577, 80573, 80572, 80571, 80570, 80569, 80567, 80566, 80564, 80563, 80562, 80561, 80560, 80559, 80558, 80557, 80556, 80554, 80552, 80551, 80549, 80548, 80547, 80545, 80543, 80542, 80540, 80539, 80538, 80537, 80536, 80535, 80534, 80533, 80532, 80531, 80523, 80522, 80521, 80520, 80519, 80518, 80517, 80516, 80515, 80512, 80511, 80509, 80508, 80506, 80503, 80501, 80500, 80499, 80498, 80497, 80496, 80495, 80494, 80491, 80490, 80489, 80488, 80487, 80486, 80485, 80484, 80483, 80480, 80479, 80478, 80477, 80476, 80475, 80474, 80473, 80470, 80469, 80468, 80467, 80466, 80465, 80464, 80463, 80462, 80461, 80460, 80454, 80453, 80452, 80451, 80449, 80444, 80443, 80442, 80441, 80439, 80436, 80435, 80434, 80433, 80432, 80431, 80430, 80429, 80428, 80426, 80425, 80424, 80423, 80422, 80421, 80420, 80419, 80418, 80416, 80415, 80414, 80413, 80412, 80411, 80410, 80409, 80408, 80404, 80403, 80402, 80401, 80400, 80399, 80398, 80396, 80395, 80393, 80392, 80391, 80390, 80389, 80388, 80387, 80386, 80385, 80384, 80383, 80382, 80381, 80380, 80379, 80378, 80377, 80375, 80374, 80373, 80372, 80371, 80370, 80369, 80368, 80366, 80365, 80364, 80363, 80359, 80357, 80356, 80355, 80354, 80352, 80351, 80350, 80349, 80336, 80335, 80334, 80332, 80331, 80330, 80329, 80327, 80326, 80325, 80324, 80323, 80322, 80321, 80318, 80315, 80314, 80310, 80309, 80308, 80301, 80299, 80297, 80295, 80294, 80293, 80290, 80288, 80287, 80286, 80283, 80282, 80281, 80280, 80279, 80277, 80276, 80275, 80274, 80270, 80268, 80267, 80266, 80265, 80264, 80263, 80262, 80260, 80258, 80251, 80250, 80249, 80248, 80247, 80246, 80244, 80243, 80241, 80240, 80237, 80236, 80235, 80234, 80232, 80231, 80230, 80228, 80227, 80226, 80223, 80221, 80217, 80215, 80214, 80213, 80212, 80211, 80209, 80208, 80207, 80206, 80204, 80203, 80201, 80200, 80196, 80194, 80193, 80191, 80189, 80186, 80184, 80183, 80182, 80181, 80180, 80179, 80177, 80176, 80174, 80173, 80172, 80170, 80169, 80166, 80165, 80162, 80159, 80158, 80157, 80156, 80155, 80154, 80153, 80152, 80150, 80149, 80147, 80146, 80145, 80144, 80143, 80142, 80141, 80140, 80139, 80138, 80137, 80136, 80135, 80134, 80133, 80128, 80127, 80126, 80125, 80124, 80122, 80121, 80120, 80118, 80117, 80116, 80115, 80114, 80113, 80112, 80111, 80110, 80109, 80108, 80107, 80106, 80105, 80104, 80103, 80102, 80101, 80100, 80099, 80093, 80092, 80090, 80089, 80088, 80085, 80083, 80081, 80080, 80079, 80078, 80077, 80076, 80075, 80074, 80072, 80071, 80070, 80069, 80068, 80067, 80066, 80065, 80064, 80063, 80062, 80061, 80060, 80059, 80058, 80052, 80049, 80048, 80047, 80043, 80042, 80041, 80038, 80037
        ];
        //80178，80175，80168，80167 2023年2月23日17:45
        if (in_array($mac_id, $mac_arr)) {
            $mac_id = "";
        }
        //系统配置
        $sys_config = config('sys_config');

        // Log::write($_SERVER['HTTP_USER_AGENT'], '请求异常');
        // var_dump($mac_id);die;
        //判断请求地址
        if ($domain != env('ApiDomainV2')) {
            header("Location:" . env('ApiDomainV2') . '?mac_id=' . $mac_id);
            return $this->returnView();
        }
        //判断设备地址
        if ($mac_id) {
            //写入cookie记录设备地址
            Cookie::set($this->CookieKey, $mac_id,300);
            if (!isset($_SERVER['HTTP_USER_AGENT'])) {
                return $this->returnView();
            }
            //判断用户微信客户端
            if (str_contains($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')) {
                //获取当前的取袋路径
                $Strategy = ScanBagStrategyModel::checkMacIdWay($mac_id);
                if($Strategy){
                    Cookie::delete('run_strategy');
                    Cookie::set('run_strategy', '1',180);
                    $redirect_uri= env('scan_bag.redirect_uri');
                }else{
                    $WayInfo = ScanBagWay::getMacIdWay($mac_id);
                    if ($WayInfo and isset($WayInfo['redirect_uri'])) {
                        $redirect_uri = $WayInfo['redirect_uri'];
                    }
                }
                //微信客户端
                $obj = new WsOfficialAccount();
                $param['appid'] = $obj->appid;
                $param['redirect_uri'] = urlencode($redirect_uri);
                $param['response_type'] = 'code';
                $param['scope'] = 'snsapi_userinfo';
                $param['state'] = $mac_id . "#wechat_redirect";
                $authorizeUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?' . urldecode(http_build_query($param));
                // Log::write($authorizeUrl, '请求异常');
                header("Location: {$authorizeUrl}");

                return $this->returnView();
            }
            //判断用户抖音客户端
            if (str_contains($_SERVER['HTTP_USER_AGENT'], 'aweme')) {
                //抖音客户端
                //string | 应用唯一标识
                $param['client_key'] = $sys_config['WsScanBag']['douyin_client_key'];
                //string | 设置为code这个字符串即可
                $param['response_type'] = "code";
                //string | 应用授权作用域,多个授权作用域以英文逗号（,）分隔
                $param['scope'] = "user_info,mobile_alert,video.create,data.external.item";
                //string | 授权成功后的回调地址，必须以http/https开头。域名必须对应申请应用时填写的域名，如不清楚请联系应用申请人。
                $param['redirect_uri'] = urlencode($sys_config['WsScanBag']['douyin_redirect_uri']);
                //string | 用于保持请求和回调的状态
                $param['state'] = ['mac_id' => $mac_id, 'nonceStr' => uuid(), 'timestamp' => time(),];
                //安全签名规则
                $param['state']['signature'] = secureSignature($param['state'], 'Z_TiRRWWhQgVyVnSGmHh6ZiFVIySzt3gM9GSO0E');
                //跳转地址
                $authorizeUrl = 'https://open.douyin.com/platform/oauth/connect?' . urldecode(http_build_query($param));
                header("Location: {$authorizeUrl}");
                return $this->returnView();
            }
        }
        return $this->returnView();
    }

    /**
     * 微信取袋扫码跳转页
     * @return string
     */
    public function wechatJump(): string
    {
        //接收参数
        $domain = $this->request->domain();
        //判断请求地址
        if ($domain != env('ApiDomainV2')) {
            header("Location:" . env('ApiDomainV2') . '/scan_bag/wechatJump');
            return $this->returnView();
        }
        //判断是否存在设备ID
        $run_strategy = Cookie::get('run_strategy');
        if($run_strategy && $run_strategy==1){
            $redirect_uri= env('scan_bag.wechat_jump_uri');
            header("Location:" . $redirect_uri);
        }else{
            $mac_id = Cookie::get($this->CookieKey);
            if ($mac_id) {
                $WayInfo = ScanBagWay::getMacIdWay($mac_id);
                if (isset($WayInfo['jump_uri']) and $WayInfo['jump_uri']) {
                    header("Location:" . $WayInfo['jump_uri']);
                }
            }
        }

        return $this->returnView();
    }

    /**
     * 统一返回页面提示
     * @return string
     */
    private function returnView(): string
    {
        //提示
        View::assign([
            'code' => 0,
            'msg' => '正在建设中，敬请期待！'
        ]);
        return View::fetch('take_bag/wechat_jump');
    }

    ####################################### 第三方客户端出袋 ##########################################

    /**
     * 无塑互联小程序出袋
     * @return Json
     */
    public function appletOutBag(): Json
    {
        //接收参数
        $way = $this->request->param('way/s', '');
        $params['waycode'] = $this->request->param('waycode/s', '');
        if(!$params['waycode']){
            $params['way_code'] = $this->request->param('way_code/s', '');
        }
        $params['nonceStr'] = $this->request->param('nonceStr/s', '');
        $params['timestamp'] = $this->request->param('timestamp/d', 0);
        $sign = $this->request->param('sign/s', '');
        //验证签名
        if($way){
            $params['way'] = $way;
        }
        $verify_sign = secureSignature($params, 'da4784bbf36c7632a6c3bf443fbc3951');
        if ($verify_sign != $sign) {
            return jsonReturn(0, [], '签名验证未通过');
        }
        if(!$way){
            $params['way'] = 'V2_Way_2_0';
        }
        //验证时间戳事件
        $time = time();
        if (($params['timestamp']-$time) >20  or $params['timestamp'] < ($time - 180)) {
            return jsonReturn(0, [], '时间戳验证未通过');
        }
        //根据不同的路径进行出袋
        switch ($params['way']) {
            case 'V1_Way_1_0':
            case 'V1_Way_1_1':
            case 'V1_Way_2_0':
            case 'V1_Way_3_0':
            case 'V1_Way_4_0':
            case 'V1_Way_4_1':
            case 'V1_Way_4_4':
            case 'V1_Way_4_4_1':
            case 'V1_Way_4_4_2':
                //小程序出袋
                $Service = new ScanBagV1Service();
                return $Service->outBagWaycodeToV1($params['waycode'], $params['way']);
            case 'V1_Way_4_5':
            case 'V1_Way_6_0':
                //小程序出袋
                $Service = new ScanBagV1Service();
                return $Service->outBagWaycodeToV1($params['waycode'], $params['way']);
            case 'V2_Way_2_0':
            case 'LCV3_0':
            case 'LCV3_1':
                //小程序出袋
                $res = ScanBagV2Service::bagOutProgram($params['waycode'], $params['way']);
                if ($res['bool']) {
                    return jsonReturn(1, $res['data'], $res['msg']);
                }
                return jsonReturn(0, [], $res['msg']);
            default:
                break;
        }
        return jsonReturn(0, [], '出袋失败,路径参数错误!');
    }

    /**
     * 第三方平台获取取袋次数
     * @return Json
     */
    public function wechatScanBagNum(): Json
    {
        //接收参数
        $params['mac_id'] = $this->request->param('mac_id/s', '');
        $params['unionid'] = $this->request->param('unionid/s', '');
        $params['nickname'] = $this->request->param('nickname/s', '');
        $params['avatar'] = $this->request->param('avatar/s', '');
        $params['nonceStr'] = $this->request->param('nonceStr/s', '');
        $params['timestamp'] = $this->request->param('timestamp/d', 0);
        $sign = $this->request->param('sign/s', '');
        //判断参数
        if (!$params['mac_id'] or !$params['unionid'] or !$params['nickname'] or !$params['nonceStr'] or !$params['timestamp']) {
            return jsonReturn(0, [], '参数错误');
        }
        //验证签名
        $verify_sign = secureSignature($params, 'f133ed9d83a7189ef3ef002ec0ce34fd');
        if ($verify_sign != $sign) {
            return jsonReturn(0, [], '签名验证未通过');
        }
        //验证时间戳事件
        $time = time();
        if ($params['timestamp'] > $time or $params['timestamp'] < ($time - 60)) {
            return jsonReturn(0, [], '时间戳验证未通过');
        }
        //判断用户是否存在
        $User = User::where(['unionid' => $params['unionid']])->find();
        if (!$User) {
            $User = new User();
            $params['uuid'] = uuid();
            $params['create_time'] = time();
            $params['update_time'] = time();
            $User->save($params);
        }
        //获取取袋次数
        $user_id = intval($User->id);
        //获取路径
        $WayInfo = ScanBagWay::getMacIdWay($params['mac_id']);
        $str = substr($WayInfo['key'], 0, 2);
        switch ($str) {
            case 'V1':
                $server = new ScanBagV1Service();
                $scan_num = $server->userScanNum2($user_id, $params['mac_id']);
                if (!$scan_num['bool']) {
                    return jsonReturn(0, [], $scan_num['msg']);
                }
                $day['daily_limit'] = $scan_num['data']['global_task_num'];
                $day['day_used'] = $scan_num['data']['finish_task_num'];
                $day['day_surplus'] = $day['daily_limit'] - $day['day_used'];
                break;
            case  'V2':
                $day = ScanBagDay::getUserIdDayNum($user_id);
                break;
            default:
                return jsonReturn(0, [], '设备路径错误');
        }
        return jsonReturn(1, $day);
    }

    /**
     * 第三方微信出袋
     * @return Json
     */
    public function wechatPostOutBag(): Json
    {
        //接收参数
        $params['mac_id'] = $this->request->param('mac_id/s', '');
        $params['unionid'] = $this->request->param('unionid/s', '');
        $params['nonceStr'] = $this->request->param('nonceStr/s', '');
        $params['timestamp'] = $this->request->param('timestamp/d', 0);
        $sign = $this->request->param('sign/s', '');
        //判断参数
        if (!$params['mac_id'] or !$params['unionid'] or !$params['nonceStr'] or !$params['timestamp']) {
            return jsonReturn(0, [], '参数错误');
        }
        //验证签名
        $verify_sign = secureSignature($params, 'f133ed9d83a7189ef3ef002ec0ce34fd');
        if ($verify_sign != $sign) {
            return jsonReturn(0, [], '签名验证未通过');
        }
        //验证时间戳事件
        $time = time();
        if ($params['timestamp'] > $time or $params['timestamp'] < ($time - 60)) {
            return jsonReturn(0, [], '时间戳验证未通过');
        }
        //获取用户信息
        $User = User::where(['unionid' => $params['unionid']])->find();
        if (!$User) return jsonReturn(0, [], '用户不存在');
        $User = $User->toArray();
        //获取路径
        $WayInfo = ScanBagWay::getMacIdWay($params['mac_id']);
        //生成取袋码
        $waycode = ScanBagV2Service::generateBagOutWaycode($User, $WayInfo['key'], $params['mac_id']);
        //根据不同设备版本进行出袋
        $str = substr($WayInfo['key'], 0, 2);
        switch ($str) {
            case 'V1':
                $Service = new ScanBagV1Service();
                return $Service->outBagWaycodeToV1($waycode, $WayInfo['key']);
            case  'V2':
                $res = ScanBagV2Service::bagOutProgram($waycode, $WayInfo['key']);
                if ($res['bool']) {
                    return jsonReturn(1, $res['data'], $res['msg']);
                }
                return jsonReturn(0, $res['data'], $res['msg']);
        }
        return jsonReturn(0, [], '出袋失败');
    }


    public function doTask(): string
    {
        $params['task_code'] = $this->request->param('task_code/s', '');
        $params['nonceStr'] = $this->request->param('nonceStr/s', '');
        $params['timestamp'] = $this->request->param('timestamp/d', 0);
        $sign = $this->request->param('sign/s', '');
        $verify_sign = secureSignature($params, 'da4784bbf36c7632a6c3bf443fbc3951');
        if ($verify_sign != $sign) {
//            return jsonReturn(0, [], '签名验证未通过');
        }
        //验证时间戳事件
        $time = time();
        if (($params['timestamp']-$time) >20  or $params['timestamp'] < ($time - 180)) {
//            return jsonReturn(0, [], '时间戳验证未通过');
        }
        return (new V1_Way_1_0Service())->doIntegralTask($params['task_code']);
    }

    /**
     * 获取JSAPI配置
     * @return Json
     */
    public function jsApiConfig(): Json
    {
        //接受参数
        $url = $this->request->param('url/s', '');
        $obj = new WsOfficialAccount();
        $data = $obj->getJsApi($url);;
        return jsonReturn(1, $data);
    }

}
